package com.k12.user.service.impl;

import com.k12.user.model.entity.SysUserRole;
import com.k12.user.mapper.SysUserRoleDao;
import com.k12.user.service.SysUserRoleService;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;
/**
 * 用户和角色关联表(SysUserRole)表服务实现类
 *
 * @author makejava
 * @since 2025-07-28 20:53:08
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl implements SysUserRoleService {
    @Resource
    private SysUserRoleDao sysUserRoleDao;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public SysUserRole queryById(Long userId) {
        return this.sysUserRoleDao.queryById(userId);
    }

    /**
     * 分页查询
     *
     * @param sysUserRole 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public List<SysUserRole> queryByPage(SysUserRole sysUserRole) {
        return this.sysUserRoleDao.queryAllByLimit(sysUserRole);
    }

    /**
     * 新增数据
     *
     * @param sysUserRole 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserRole insert(SysUserRole sysUserRole) {
        this.sysUserRoleDao.insert(sysUserRole);
        return sysUserRole;
    }

    /**
     * 修改数据
     *
     * @param sysUserRole 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserRole update(SysUserRole sysUserRole) {
        this.sysUserRoleDao.update(sysUserRole);
        return this.queryById(sysUserRole.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long userId) {
        return this.sysUserRoleDao.deleteById(userId) > 0;
    }

    @Override
    public void addUserRole(List<SysUserRole> sysUserRole) {
        this.sysUserRoleDao.insertBatch(sysUserRole);
    }

    @Override
    public List<Long> getRoleIds(String userId, String tenantId) {
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(Long.valueOf(userId));
        sysUserRole.setTenantId(Long.valueOf(tenantId));
        List<SysUserRole> sysUserRoles = sysUserRoleDao.queryAllByLimit(sysUserRole);
        return sysUserRoles.stream().map(SysUserRole::getRoleId).toList();
    }

    @Override
    public void deleteUserRoleByTenantId(Long id, String tenantId) {
        sysUserRoleDao.deleteUserRoleByTenantId(id, tenantId);
    }

    @Override
    public int saveSysUserRole(List<SysUserRole> list) {
        return sysUserRoleDao.insertBatch(list);
    }

    @Override
    public int deleteAuthUser(SysUserRole userRole) {
        return sysUserRoleDao.deleteAuthUser(userRole);
    }
}
